<?php
/**
 * Created by PhpStorm.
 * User: wumengmeng <wu_mengmeng@foxmail.com>
 * Date: 2019/4/25 0025
 * Time: 17:46
 */

//////////////////////// 管理后台 start ////////////////////////
use HiCommon\Repository\ShopRepository;

/**
 * admin 模块-登录认证guard
 *
 *
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function admin_guard()
{
    return guard('admin');
}

/**
 * admin 模块-获取用户信息
 *
 *
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function admin_user()
{
    return admin_guard()->user();
}

/**
 * admin 模块-用户id
 *
 *
 * @return int|string|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function admin_user_id()
{
    return admin_guard()->id();
}

/**
 * admin 模块-注销用户
 *
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function admin_offset_user()
{
    admin_guard()->logout();
    session_clear();
}
//////////////////////// 管理后台 end ////////////////////////



//////////////////////// 用户端-小程序 start ////////////////////////
/**
 * small_api 模块-登录认证guard
 *
 *
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function small_api_guard()
{
    return guard('small_api');
}

/**
 * small_api 模块-获取用户信息
 *
 *
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function small_api_user()
{
    return small_api_guard()->user();
}

function small_api_user_openid(){
    return small_api_user()->small_openid;
}

/**
 * small_api 模块-用户id
 *
 *
 * @return int|string|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function small_api_user_id()
{
    return small_api_guard()->id();
}

/**
 * small_api 模块-注销用户
 *
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function small_api_offset_user()
{
    small_api_guard()->logout();
    session_clear();
}
//////////////////////// 用户端-小程序 end ////////////////////////



//////////////////////// 商户端-公众号 start ////////////////////////
/**
 * 获取公众号openid
 *
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function get_openid()
{
    $wechat = new \HiCommon\Vendor\WechatOfficialAccount();
    return $wechat->GetOpenid();
}

/**
 * 微信公众号-获取wat token
 *
 *
 * @return array|string
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function get_wat_token(){
    return  Request::header('token');
}

/**
 * 获取公众号用户信息
 *
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function wechat_user()
{
    $openid = get_wat_token();

    $arr_where = ['openid'=>$openid];
    $res = \HiCommon\Service\AdminUserService::api_get_admin_user_info_by_where($arr_where);
    if($res['state'] != 0){
        $data = [];
    }
    else{
        $data = $res['data'];
    }


    /*    $key  = 'wechat_'.$openid;
        $data = Cache::get($key);
        if(is_null($data) || empty($data)){
            $arr_where = ['openid'=>$openid];
            $res = \HiCommon\Service\AdminUserService::api_get_admin_user_info_by_where($arr_where);
            if($res['state'] != 0){
                $data = [];
            }
            else{
                $data = $res['data'];
            }
            Cache::set($key,$data,60*60*2);

        }*/
    return $data;
}


function wechat_user_id(){
    $wechat_user = wechat_user();
    $wechat_user_id = null;
    if(isset($wechat_user['id'])){
        $wechat_user_id = intval($wechat_user['id']);
    }
    return $wechat_user_id;
}

function wechat_shop_id()
{
    $admin_id = wechat_user_id();
    $store = ShopRepository::get_where_one(['admin_id' => $admin_id]);
    if($store){
        return $store->id;
    }else{
        return null;
    }
}

function wechat_shop()
{
    $admin_id = wechat_user_id();
    $res = ShopRepository::get_where_one(['admin_id' => $admin_id]);
    if(!is_null($res)){
        return $res->toarray();
    }else{
        return [];
    }
}
//////////////////////// 商户端-公众号 end ////////////////////////



//////////////////////// 商户端-App start ////////////////////////
/**
 * web 模块-登录认证guard
 *
 *
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function web_guard()
{
    return guard('web');
}

/**
 * web 模块-获取用户信息
 *
 *
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function web_user()
{
    return web_guard()->user();
}

/**
 * web 模块-用户id
 *
 *
 * @return int|string|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function web_user_id()
{
    return web_guard()->id();
}


/**
 * web 模块-注销用户
 *
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function web_offset_user()
{
    web_guard()->logout();
    session_clear();
}

function get_web_shop_id()
{
    return intval(web_user()->shop_id);
}

function get_shop_cate_by_shop_userid($n_uid)
{
    $n_shop_cate = \HiCommon\Model\Shop::whereHas('shop_user',
      function ($query)
      use ($n_uid) {
          $query->where('id', $n_uid);
      })
                                       ->first(['id', 'cate'])->cate;
    return intval($n_shop_cate);
}

/*根据用户id获取商户信息*/
function get_shop_by_id($n_id,$arr_field = ['*'])
{
    $result = \HiCommon\Model\Shop::select($arr_field)->find($n_id);
    if(is_null($result)){
        return [];
    }
    else{
        return $result->toarray();

    }
}

//////////////////////// 商户端-App end ////////////////////////




//////////////////////// 商户后台 start ////////////////////////
/**
 * shop 模块-登录认证guard
 *
 *
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function shop_guard()
{
    return guard('shop');
}

/**
 * shop 模块-获取用户信息
 *
 *
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function shop_user()
{
    return shop_guard()->user();
}

/**
 * shop 模块-用户id
 *
 *
 * @return int|string|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function shop_user_id()
{
    return shop_guard()->id();
}

/**
 * shop 模块-注销用户
 *
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function shop_offset_user()
{
    shop_guard()->logout();
    session_clear();
}

function shop_shop_id()
{
    return intval(shop_user()->shop_id);
}

function shop_phone()
{
    return shop_user()->phone;
}

//////////////////////// 商户后台 end ////////////////////////


//////////////////////// 用户端-App start ////////////////////////
/**
 * api 模块-登录认证guard
 *
 *
 * @return \Illuminate\Contracts\Auth\Guard|\Illuminate\Contracts\Auth\StatefulGuard|mixed
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function api_guard()
{
    return guard('api');
}

/**
 * api 模块-获取用户信息
 *
 *
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function api_user()
{
    return api_guard()->user();
}

/**
 * api 模块-用户id
 *
 *
 * @return int|string|null
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function api_user_id()
{
    return api_guard()->id();
}

/**
 * api 模块-注销用户
 *
 *
 * @author wumengmeng <wu_mengmeng@foxmail.com>
 */
function api_offset_user()
{
    api_guard()->logout();
    session_clear();
}
//////////////////////// 用户端-App end ////////////////////////



/**
 *  生成签名
 *
 **/
function makeSign( array $arr , string $appsecret){
    return MD5( $arr['illegalid'].$arr['mobile'].$arr['outorderno'].$appsecret);
}

/**
 *  获取交管局接口
 *
 *
 **/
function jisuapi_api_url_carorg(){
    return "https://api.jisuapi.com/illegal/carorg2?appkey=".config('system.jssj_app_key');
}

/**
 *  获取车牌类型接口
 *
 **/
function jisuapi_api_url_lstype(){
    return "https://api.jisuapi.com/illegal/lstype?appkey=".config('system.jssj_app_key');
}

/**
 *  违章查询接口
 *
 **/
function jisuapi_api_url_illegal(){
    return "https://api.jisuapi.com/illegal/query?appkey=".config('system.jssj_app_key');
}

/**
 *  违章代办提交接口
 *
 **/
function jisuapi_api_url_entrust(){
    return "https://api.jisuapi.com/illegalhandle/handle?appkey=".config('system.jssj_app_key');
}

/**
 *  违章代办提交接口
 *
 **/
function jisuapi_api_url_entrust_list(){
    return "https://api.jisuapi.com/illegalhandle/orderlist?appkey=".config('system.jssj_app_key');
}




/**
 *  极数数据官方提供的curl方法  调用到时候既然可以请求到
 *
 **/
function curlOpen($url, $config = array())
{
    $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 20, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>false,'isupfile'=>false);
    $arr = array_merge($arr, $config);
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
    curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
    curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
    curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
    //curl_setopt($ch, CURLOPT_HEADER, true);//获取header
    if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
    if($arr['ssl'])
    {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    if(!empty($arr['cookie']))
    {
        curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
    }

    if(!empty($arr['proxy']))
    {
        //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
        if(!empty($arr['userpwd']))
        {
            curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
        }
    }

    //ip比较特殊，用键值表示
    if(!empty($arr['header']['ip']))
    {
        array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
        unset($arr['header']['ip']);
    }
    $arr['header'] = array_filter($arr['header']);

    if(!empty($arr['header']))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
    }

    if ($arr['post'] != false)
    {
        curl_setopt($ch, CURLOPT_POST, true);
        if(is_array($arr['post']) && $arr['isupfile'] === false)
        {
            $post = http_build_query($arr['post']);
        }
        else
        {
            $post = $arr['post'];
        }
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    $result = curl_exec($ch);
    //var_dump(curl_getinfo($ch));
    curl_close($ch);

    return $result;
}

///**
// * 提取车辆模型简洁信息
// *
// * @param $arr_whole_vehicle_model
// *
// * @return array|null
// * @author wumengmeng <wu_mengmeng@foxmail.com>
// */
//function transfer_to_simple_vehicle_model($arr_whole_vehicle_model){
//
//    $arr_data = null;
//    if(!is_null($arr_whole_vehicle_model)){
//        $arr_data = [
//          'id'=>$arr_whole_vehicle_model['id'],
//          'one_name'=>$arr_whole_vehicle_model['parent']['parent']['name'],
//          'two_name'=>$arr_whole_vehicle_model['parent']['name'],
//          'three_name'=>$arr_whole_vehicle_model['name'],
//          'logo'=>$arr_whole_vehicle_model['parent']['parent']['parent']['logo'],
//        ];
//    }
//
//    return $arr_data;
//}

